package edu.sias.controller;

import edu.sias.pojo.Post;
import edu.sias.service.CategoryService;
import edu.sias.service.SearchService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(urlPatterns = "/category/info")
public class CategoryServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //编码格式设定
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        //获取类别id
        String s = request.getParameter("id");
        int id = Integer.parseInt(s);

        //根据类别id获取对应的文章信息
        SearchService searchService
                = new SearchService();
        List<Post> list = searchService.findPostByCategotyId(id);

        //将数据返回展示
        request.setAttribute("posts",list);
        request.getRequestDispatcher("/showMsg.jsp")
                .forward(request,response);

    }
}
